import { observable, action } from 'mobx';

import ListBaseStore from './ListBaseStore';
import * as DiscussManageApis from '../apis/discussManage';
import { WeaLocaleProvider } from 'ecCom';
import { WeaLogView } from 'comsMobx';

const getLabel = WeaLocaleProvider.getLabel;
const { LogStore } = WeaLogView;
/**
 * 评论监控页面Store
 */
export default class CommentMonitorStore extends ListBaseStore {
  @observable deleteDialogVisible = false; // 删除对话框是否显示
  @observable logStore = new LogStore();

  constructor() {
    super();

    this.title = getLabel(131886, '评论监控');
    this.selectedKey = 0;
  }

  @observable status = {
    targetId: '',
    logType: '', // 日志相关参数
    logSmallType: '',
  };

  // 方法---------------------------------

  @action doInit = () => {
    this.doBaseInit();
    this.selectedKey = 0;
    this.initConditionDatas(); // 初始化高级搜索区域
    this.doTableLoading(); // 初始化列表
    this.loadLeftTree(); // 初始化左侧树
  };

  // 重置表单状态
  @action clearStatus = () => {
    this.setShowSearchAd(false);
    this.clearFormFields();
    this.selectedTreeKeys = [];
    this.viewRightInitCompleted = false;
  };

  // 加载列表数据
  @action doTableLoading = (params) => {
    this.showLoading(true);

    let resultParams = this.getFormParams();

    let selectedTreeKey =
      this.selectedTreeKeys.length > 0 ? this.selectedTreeKeys[0] : '';
    let key = selectedTreeKey
      ? selectedTreeKey.substring(selectedTreeKey.indexOf('_') + 1)
      : '';

    let commonParams = {
      mainid:
        selectedTreeKey && selectedTreeKey.indexOf('cMainType_') != -1
          ? key
          : '', // 协作类型
      commentStatus: this.selectedKey, // 列表类型
    };

    resultParams = { ...resultParams, ...commonParams, ...params };

    DiscussManageApis.getCoworkCommentMonitorList(resultParams).then(
      (result) => {
        this.status = {
          ...this.status,
          logType: result.logType,
          logSmallType: result.logSmallType,
        };
        this.viewRight = result.viewRight == 1;
        if (this.viewRight) {
          const sessionkey = result.sessionkey;
          this.sessionkey = sessionkey;
          this.comsWeaTableStore.getDatas(sessionkey, 1);
        }

        this.setViewRightInitCompleted();
        this.showLoading(false);
      },
    );
  };

  // 初始化收藏的帖子页面查询条件
  @action initConditionDatas = (params) => {
    DiscussManageApis.getCoworkCommentMonitorShareCondition(params).then(
      (result) => {
        this.topTab = result.topTab;
        this.conditioninfo = result.conditioninfo;

        this.initFormFields(result.conditioninfo[this.selectedKey]); // 一定要先初始化initFormFields表单对象
      },
    );
  };

  // 删除、批量删除
  @action batchCommentMonitorDel = (params, callback) => {
    DiscussManageApis.batchCommentMonitorDel(params).then(() => {
      if (callback && typeof callback === 'function') {
        callback();
      }
    });
  };

  // 加载左侧 版块树
  @action loadLeftTree = (params) => {
    DiscussManageApis.getCoworkTreeDatas(params).then((result) => {
      this.leftTreeDatas = result.treeDatas;
      this.leftTreeTypes = result.treeTypes;
      this.leftTreeCounts = result.treeCounts;
    });
  };
}
